package com.invest.infra.excel.wrapper.impl;

import com.invest.infra.excel.wrapper.ExCell;
import com.invest.infra.excel.wrapper.ExSheet;
import com.invest.utils.date.DateParseHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;

import java.math.BigDecimal;
public class ExCellImpl implements ExCell {

	private ExSheet sheet;
	private Cell cell;
	private int  index;
	
	private ExCellImpl(ExSheet sheet,Cell cell,int  index){
		this.sheet = sheet;
		this.cell = cell;
		this.index = index;
	}

	public static ExCell instance(ExSheet sheet, Cell cell,int  index){
		ExCellImpl exCell = new ExCellImpl(sheet,cell,index);
		return exCell;
	}
	
	public Cell getPoiCell() {
		return cell;
	}

	public void setPoiCell(Cell cell) {
		this.cell = cell;
	}



	public String getText() {
		
		String content = "";
		
		if (null != cell) {
			int type = cell.getCellType();

			switch (type) {
				case Cell.CELL_TYPE_BLANK:
					break;
				case Cell.CELL_TYPE_NUMERIC:
					content = dealNumberic();
					break;
				case Cell.CELL_TYPE_STRING:
					content = StringUtils.trim(cell.getStringCellValue());
					break;
				case Cell.CELL_TYPE_BOOLEAN:
					content = String.valueOf(cell.getBooleanCellValue());
					break;
				default : break ;
			}
		}

		return content;

	}
	
	public String dealNumberic() {

		String content;
		if (HSSFDateUtil.isCellDateFormatted(cell)) {
			content = DateParseHelper.formatShort(cell.getDateCellValue());
		} else {
			content = new BigDecimal(cell.getNumericCellValue()).toPlainString();
		}
		return content;
	}

	@Override
	public void addComment(int row,String comment) {


		Drawing p = getSheet().getPoiSheet().createDrawingPatriarch();

		Comment cellComment = createComment(p,comment);
		if(cellComment == null){
			return ;
		}

		CellStyle cellStyle= getSheet().getWorkbook().getPoiWorkbook().createCellStyle();
		cellStyle.setFillBackgroundColor(HSSFColor.RED.index);
		cellStyle.setFillForegroundColor(HSSFColor.RED.index);
		// We will now specify a background cell color
		cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
		cellStyle.setFillForegroundColor(new HSSFColor.RED().getIndex());
		cellStyle.setFillBackgroundColor(new HSSFColor.RED().getIndex());


		if(cell == null){
			cell = sheet.getPoiSheet().getRow(row).createCell(index);
		}
		cell.setCellStyle(cellStyle);
		cell.setCellComment(cellComment);
	}

	private Comment createComment(Drawing p,String comment) {

		if(p instanceof HSSFPatriarch){

			Comment cellComment = p.createCellComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 3, 3,(short) 5, 6));
			cellComment.setString(new HSSFRichTextString(comment));
			return cellComment;

		}

		if(p instanceof XSSFDrawing){
			Comment cellComment = p.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3,(short) 5, 6));
			cellComment.setString(new XSSFRichTextString(comment));
			return cellComment;
		}

		return null;
	}

	@Override
	public ExSheet getSheet() {
		return sheet;
	}
}
